#include "random.h"

void my_omp_srand(unsigned int seed)
{
    omp_set_dynamic(0);
    next = seed;
#pragma omp parallel copyin(next)
    {
#pragma omp single nowait
        {
            num_thread = omp_get_num_threads();
        }
        next /= omp_get_thread_num() + 1;
    }
}

double my_omp_rand(void)
{
    double a = 0.0, b = 10.0;
    if ((unsigned int)omp_get_num_threads() != num_thread ||
        omp_get_dynamic() != 0)
        omp_set_num_threads(num_thread);
    return ((double)my_omp_rand_r(&next) / (double)RAND_MAX * (b - a) + a);
}

int my_omp_rand_r(unsigned int *seed)
{
    *seed = *seed * 1103515245L + 12345L;
    return (*seed % ((unsigned int)RAND_MAX + 1));
}
